package com.yc.hotel.feign.controller;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.yc.hotel.dto.OrderInfoDTO;
import com.yc.hotel.dto.RoomInfoDTO;
import com.yc.hotel.room.feign.IRoomInfoFeignClient;
import com.yc.hotel.service.IOrderInfoService;

@RestController
@RequestMapping("/orders")
public class OrderInfoFeignController {

	@Autowired
	private IOrderInfoService orderInfoService;
	
	@Autowired
	private IRoomInfoFeignClient roomInfoService;
	
	@PostMapping("/outOrder")
	public int outOrder(@RequestParam  String ono, @RequestParam  String price) {

		int result = orderInfoService.outOrder(ono, price);
		return result;
	};
	
	@PostMapping("/closeOrder")
	public int closeOrder(@RequestParam  String ono, @RequestParam String rno) {

		int result = orderInfoService.update(ono, 0);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("rno", rno);
		map.put("status", 1);
		result += roomInfoService.updateStatus(map);
		
		return result;
	};
	
	@PostMapping("/check")//商家确认订单
	public int check(@RequestParam String rno, @RequestParam  String tno, @RequestParam  String status) {
		if(status.contentEquals("2")) {//确认订单
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("rno", rno);
			map.put("status", status);
			int result = orderInfoService.update(map);				
			
			if(tno.equals("101")) {
				map.put("status", 0);
			}else {
				map.put("status", 3);
			}
			result += roomInfoService.updateStatus(map);
			return result;
		}else if(status.contentEquals("1")){//拒绝订单
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("rno", rno);
			map.put("status", 0);
			int result = orderInfoService.update(map);				
			
			map.put("status", 1);
			
			result += roomInfoService.updateStatus(map);
			return result;
		}
		
		return -1;
	};
	
	@PostMapping("/finds")
	public List<OrderInfoDTO> finds(@RequestParam Map<String, Object> map) {
		List<OrderInfoDTO> orders = orderInfoService.findRoom(map);
		for(OrderInfoDTO order : orders) {
			Integer rno = order.getRno();
			RoomInfoDTO room = roomInfoService.findByRno(rno);
			order.setPics(room.getPics());
			order.setRname(room.getRname());
			order.setPrice(room.getPrice());
			order.setTno(room.getTno());
		}
		return orders;

	};
	
	@PostMapping("/update")
	int update(@RequestParam String ono, @RequestParam int status) {
		return orderInfoService.update(ono, status);
	}
	
	@PostMapping("/endSale")
	int endSale(@RequestParam Map<String, Object> map) {
		return orderInfoService.endSale(map);
	};
	
	@PostMapping("/endRent")
	int endRent(@RequestParam Map<String, Object> map) {
		return orderInfoService.endRent(map);
	};

}
